﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package net.wooga.pets.views.kindergarten {
    import flash.events.*;
    import flash.display.*;
    import flash.geom.*;
    import flash.text.*;
    import flash.utils.*;
    import net.wooga.pets.models.*;
    import reprise.tweens.*;
    import net.wooga.pets.constants.*;
    import org.robotlegs.mvcs.*;
    import net.wooga.pets.utils.*;
    import com.robertpenner.easing.*;
    import net.wooga.pets.views.components.controls.*;

    public class KindergartenTeaserMediator extends Mediator {

		[Inject]
        public var languageModel:LanguageModel;
        private var animationTimer:Timer;
        private var textBubble:MovieClip;
        private var textField:TextField;

        private static const MIN_TIMER:int = 10000;
        private static const MAX_TIMER:int = 15000;
        private static const CLICK_TIMER:int = 3000;
        private static const TEXT_PADDING:int = 120;
        private static const TEXT_MARGIN:int = 20;
        private static const WALK_SPEED:int = 4500;
        private static const DESTINATION_X:int = 2200;
        private static const WALKING:String = "walk";
        private static const STILL:String = "still";
        private static const WAVING:String = "waving";
        private static const SLEEPING:String = "sleep";
        private static const HAPPY:String = "happy";

        override public function onRegister():void{
            this.initSpeechBubble();
            this.walkToHospital();
        }
        override public function onRemove():void{
            if (this.animationTimer){
                this.animationTimer.stop();
                this.animationTimer.removeEventListener(TimerEvent.TIMER, this.onTimerComplete);
                this.animationTimer = null;
            };
        }
        private function setAnimationTimer():void{
            this.animationTimer = new Timer(500);
            this.animationTimer.addEventListener(TimerEvent.TIMER, this.onTimerComplete);
            this.startTimer(MIN_TIMER, MAX_TIMER);
        }
        private function startTimer(_arg1:int, _arg2:int):void{
            this.animationTimer.delay = Numbers.randomIntBetween(_arg1, _arg2);
            this.animationTimer.start();
        }
        private function onTimerComplete(_arg1:TimerEvent):void{
            var _local2:int = Numbers.randomIntBetween(0, 100);
            var _local3:String = STILL;
            switch (true){
                case (_local2 < 10):
                    _local3 = WAVING;
                    break;
                case (_local2 < 20):
                    _local3 = SLEEPING;
                    break;
                case (_local2 < 30):
                    _local3 = HAPPY;
                    break;
            };
            this.view.setAnimationState(_local3, true);
            this.startTimer(MIN_TIMER, MAX_TIMER);
        }
        private function walkToHospital():void{
            this.view.x = -1000;
            this.view.setAnimationState(WALKING, true);
            var _local1:SimpleTween = new SimpleTween(WALK_SPEED);
            _local1.addTweenProperty(this.view, "x", this.view.x, DESTINATION_X, Quad.easeOut);
            _local1.addEventListener(Event.COMPLETE, this.onWalkComplete);
            _local1.execute();
        }
        private function onWalkComplete(_arg1:Event):void{
            if (this.view){
                eventMap.mapListener(this.view, MouseEvent.CLICK, this.onClick);
                eventMap.mapListener(this.view, MouseEvent.MOUSE_OVER, this.onMouseOver);
                eventMap.mapListener(this.view, MouseEvent.MOUSE_OUT, this.onMouseOut);
                this.view.setAnimationState(STILL, true);
                this.setAnimationTimer();
            };
        }
        private function onClick(_arg1:MouseEvent):void{
            this.animationTimer.stop();
            this.onMouseOut();
            this.view.setAnimationState(WAVING, true);
            this.startTimer(CLICK_TIMER, CLICK_TIMER);
        }
        private function onMouseOver(_arg1:MouseEvent):void{
            this.view.mouseOverEffect();
            this.showBubble();
        }
        private function onMouseOut(_arg1:MouseEvent=null):void{
            this.view.mouseOutEffect();
            this.view.removeSpeechBubble();
        }
        private function initSpeechBubble():void{
            var _local1:String = this.languageModel.getTextByKey("kindergarten_teaser_bubble");
            this.textBubble = AssetsLibrary.getMovieClip(SpeechBubbleConstants.TEXT_BUBBLE);
            this.textField = new SpeechBubbleTextfield("0x000000", 54);
            this.textField.htmlText = _local1;
            this.textField.multiline = true;
            this.textField.width = 550;
            this.textField.wordWrap = true;
            this.textField.autoSize = TextFieldAutoSize.LEFT;
            this.textBubble.addChild(this.textField);
            this.view.speechBubblePosition = new Point(0, -200);
        }
        private function showBubble():void{
            var _local1:int = ((SpeechBubbleConstants.KINDERGARTEN_TEASER_BUBBLE_MARGIN_LEFT + this.textField.width) + SpeechBubbleConstants.KINDERGARTEN_TEASER_BUBBLE_MARGIN_RIGHT);
            var _local2:int = (this.textField.height + SpeechBubbleConstants.KINDERGARTEN_TEASER_MARGIN_TOP);
            this.textBubble.background.width = _local1;
            this.textBubble.background.height = _local2;
            this.textField.x = (-(_local1) + SpeechBubbleConstants.KINDERGARTEN_TEASER_BUBBLE_MARGIN_LEFT);
            this.textField.y = ((-(_local2) + SpeechBubbleConstants.KINDERGARTEN_TEASER_MARGIN_TOP) + SpeechBubbleConstants.KINDERGARTEN_TEASER_BUBBLE_OFFSET_TOP);
            this.view.addSpeechBubble(this.textBubble);
        }
        private function get view():KindergartenTeaserDisplay{
            return ((viewComponent as KindergartenTeaserDisplay));
        }

    }
}//package net.wooga.pets.views.kindergarten 
